//
// type MembersList -- list of community members
//
function MembersList(dom_elt, cfg) {
		this.view = new MembersListView(dom_elt);
		if (typeof cfg == 'object') for (var i in cfg) this[i] = cfg[i];
}

MembersList.prototype.bind = function() {
		jQuery(this.view.getMembersList()).each(function (i, dom_elt) {
			(new Member(dom_elt)).bind();
		});
		this.view._bind(this);
}



//
// type Member -- community member
//
function Member(dom_elt, cfg) {
		this.view = new MemberView(dom_elt);
		if (typeof cfg == 'object') for (var i in cfg) this[i] = cfg[i];
}

Member.prototype.bind = function() {
		//window.console.log('Member bind');
		this.view._bind(this);
}

Member.prototype.getUserId = function () {
		return this.view.getUserId();
}

Member.prototype.getRole = function () {
		return this.view.getRoleSelector().value;
}

Member.prototype.onChangeRole = function (event) {
		event.preventDefault();
		var _this = event.data;
		var cp = new cpaint();
		cp.set_env_object(_this);

		_this.view.notifyChange();

		cp.call(_this.view.getAjaxUrl(), 'ajax_doChangeUserInfo', function (result) {
			if (result.ajaxResponse['0'].status['0'].data=='0') {
				_this.view.notifyChanged();
			} else {
				_this.view.notifyError();
			}
		},
		_this.getUserId(),
		_this.getRole());
}

Member.prototype.onDelete = function (event) {
		event.preventDefault();
		var _this = event.data;
		var cp = new cpaint();
		cp.set_env_object(_this);

		_this.view.notifyChange();

		cp.call(_this.view.getAjaxUrl(), 'ajax_doExorcizeUser', function (result) {
			var r = result.ajaxResponse['0'];
			var code = r.status['0'].data;
			switch (code) {
			    case '0': // moderator removes other user
				    _this.view.notifyChanged();
				    _this.view.rmPanel();
				    break;
			    case '1': // moderator removes self
				    _this.view.notifyError();
				    window.location = r.data['0'].data;
				    break;
			    case '-1': // server operation error
			    default:
				    _this.view.notifyError();
			}
		},
		_this.getUserId()
		);
}

